package com.viish.android.apps.buildbot.job;

/* Android Buildbot Manager
 Copyright (C) 2012  Sylvain "Viish" Berfini

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.sql.Timestamp;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

public class Build
{
	private String		builderName;
	private int			number;
	private String		reason;
	private String		slaveName;
	private int			results;
	private List<Step>	steps;
	private Timestamp	time_start, time_end;
	private String		text;

	public Build(String name, int number, String reason, String slave,
			int results, List<Step> steps, Timestamp timeStart,
			Timestamp timeEnd, String text)
	{
		super();
		this.builderName = name;
		this.number = number;
		this.reason = reason;
		this.slaveName = slave;
		this.results = results;
		this.steps = steps;
		this.time_start = timeStart;
		this.time_end = timeEnd;
		this.text = text;
	}

	// TODO : Steps
	public Build(JSONObject jsono)
	{
		super();
		this.builderName = jsono.optString("builderName", null);
		this.number = jsono.optInt("number", -1);
		this.reason = jsono.optString("reason", null);
		this.slaveName = jsono.optString("slave", null);
		try
		{
			this.text = jsono.getJSONArray("text").toString().replace("[", "")
					.replace("]", "").replace(",", " ");
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}

		try
		{
			String[] timestamps = jsono.getJSONArray("times").toString()
					.replace("[", "").replace("]", "").split(",");
			this.time_start = BuildbotJSONParser.parseJSONTimeStamp(timestamps[0]);
			this.time_end = BuildbotJSONParser.parseJSONTimeStamp(timestamps[1]);
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}
		catch (NumberFormatException nfe)
		{
			nfe.printStackTrace();
		}
	}

	public String toString()
	{
		String returned = "\"" + number + "\": { " + "\"builderName\": \""
				+ builderName + "\", " + "\"number\": " + number + ", "
				+ "\"reason\": " + reason + ", " + "\"slave\": " + slaveName
				+ ", " + "\"text\": " + text;
		if (time_start != null && time_end != null)
		{
			returned += ", \"times\": [" + time_start.toLocaleString() + ", "
					+ time_end.toLocaleString() + "] }";
		}
		else
		{
			returned += " }";
		}

		return returned;
	}

	public String getBuilderName()
	{
		return builderName;
	}

	public void setBuilderName(String name)
	{
		this.builderName = name;
	}

	public int getNumber()
	{
		return number;
	}

	public void setNumber(int number)
	{
		this.number = number;
	}

	public String getReason()
	{
		return reason;
	}

	public void setReason(String reason)
	{
		this.reason = reason;
	}

	public String getSlaveName()
	{
		return slaveName;
	}

	public void setSlaveName(String slave)
	{
		this.slaveName = slave;
	}

	public int getResults()
	{
		return results;
	}

	public void setResults(int results)
	{
		this.results = results;
	}

	public List<Step> getSteps()
	{
		return steps;
	}

	public void setSteps(List<Step> steps)
	{
		this.steps = steps;
	}

	public Timestamp getTime_start()
	{
		return time_start;
	}

	public void setTime_start(Timestamp timeStart)
	{
		time_start = timeStart;
	}

	public Timestamp getTime_end()
	{
		return time_end;
	}

	public void setTime_end(Timestamp timeEnd)
	{
		time_end = timeEnd;
	}

	public String getText()
	{
		return text;
	}

	public void setText(String text)
	{
		this.text = text;
	}
}
